package com.alibaba.aone.casestudy.module.action;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.aone.casestudy.common.entity.Failure;
import com.alibaba.aone.casestudy.common.entity.FailureConditions;
import com.alibaba.aone.casestudy.service.FailureService;
import com.alibaba.citrus.turbine.Context;
import com.alibaba.citrus.turbine.dataresolver.Param;

/**
 * 类FailureListAction.java的实现描述：TODO 类实现描述
 * 
 * @author yongchun.chengyc 2011-4-27 下午01:15:23
 */
public class FailureListAction {
	@Autowired
	private FailureService failureService;

	public void doSelect(
			@Param("correlationDepa")String correlationDepa,
	@Param("rpDepartment")String rpDepartment, 
	@Param("rpMan")String rpMan,
	@Param("level")Integer[] level, 
	@Param("status")Integer[] statusArray, 
	@Param("gmtStart")String start, @Param("gmtEnd")
	String end, Context context) throws ParseException {
		FailureConditions failureConditions = new FailureConditions();

		Date gmtStart = null;
		Date gmtEnd = null;

		
		
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm");

		gmtStart = StringUtils.isEmpty(start) ? null : format.parse(start);
		gmtEnd = StringUtils.isEmpty(end) ? null : format.parse(end);

		System.out.println(gmtStart);
		if ("任意".equals(correlationDepa)) {
			correlationDepa = null;
		}
		if ("任意".equals(rpDepartment)) {
			rpDepartment = null;
		}

		failureConditions.setRpMan(rpMan);

		failureConditions.setCorrelationDepa(correlationDepa);

		failureConditions.setRpDepartment(rpDepartment);

		failureConditions.setGmtStart(gmtStart);
		failureConditions.setGmtEnd(gmtEnd);

		List<Integer> statuses = new ArrayList<Integer>();

		statuses.addAll(Arrays.asList(statusArray));

		List<Integer> ranks = new ArrayList<Integer>();
		ranks.addAll(Arrays.asList(level));

		failureConditions.setRanks(ranks);
		failureConditions.setStatuses(statuses);
		List<Failure> failures = failureService.selectFailure(failureConditions);

		context.put("failureConditions", failureConditions);
		context.put("failures", failures);

	}

}
